package oriol.forns.gutierrez;

import java.util.ArrayList;
import java.util.Random;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Powerups {

	private Random rand;
	private ArrayList<Objecte> powers;
	BitmapFactory.Options options;
	private ArrayList<Bitmap> img;
	
	
	public Powerups(Resources res){
		powers = new ArrayList<Objecte>();
		img = new ArrayList<Bitmap>();
		img.add(BitmapFactory.decodeResource(res, R.drawable.pfb, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.ppetitap, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.p1up, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.pf, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.pgranp, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.psb, options));
		img.add(BitmapFactory.decodeResource(res, R.drawable.pst, options));
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		rand = new Random();
	}

	public void actualitza() {
		for (int i = 0; i < powers.size(); ++i) {
			powers.get(i).actualitza();
		}
	}
	
	public void clean(){
		powers.clear();
	}
	public void nou_power(float bx, float by, float tbx){
		if(rand.nextInt(8)==0){
			float x, y, tx, ty;
			tx = ty = 30;
			x = bx;
			y = by;
			Objecte o;
			if(rand.nextInt(8)==0){
				if(rand.nextInt(2)==0) {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(0), "pfb");
				}
				else {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(1), "ppetitap");
				}
			}
			else {
				int r = rand.nextInt(5);
				if (r == 0) {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(2), "p1up");
				}
				else if (r == 1) {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(3), "pf");
				}
				else if (r == 2) {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(4), "pgranp");
				}
				else if (r == 3) {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(5), "psb");
				}
				else {
					o = new Objecte(x, y, 0, 7, tx, ty, img.get(6), "pst");
				}
			}
			powers.add(o);
		}
	}
	
	public void draw(Canvas canvas) {
		for (int i = 0; i < powers.size(); ++i) {
			powers.get(i).draw(canvas);
		}
	}
	
	public void colisions(float bx, float by, float btx, float bty, Pilota pilota, Base base) {
		for (int i = 0; i < powers.size(); ++i) {
			if(powers.get(i).y>GV.Screen.metrics.heightPixels+powers.get(i).ty) {
				powers.remove(i);
			}
			else if(powers.get(i).x+powers.get(i).tx>bx && powers.get(i).x<bx+btx && powers.get(i).y+powers.get(i).ty>by && powers.get(i).y<by+bty) {
				if (powers.get(i).tipus.equals("ppetitap")) {
					base.tam(false);
					GV.marcador.punts-=50;
				}
				else if (powers.get(i).tipus.equals("pgranp")) {
					GV.marcador.punts+=50;
					base.tam(true);
				}
				else if (powers.get(i).tipus.equals("p1up")) {
					GV.marcador.punts+=50;
					++GV.marcador.vides;
				}
				else if (powers.get(i).tipus.equals("pf")) {
					GV.marcador.punts+=50;
					pilota.fire();
				}
				else if (powers.get(i).tipus.equals("pfb")) {
					GV.marcador.punts-=50;
					pilota.vel(2);
				}
				else if (powers.get(i).tipus.equals("psb")) {
					GV.marcador.punts+=50;
					pilota.vel(-2);
				}
				else if (powers.get(i).tipus.equals("pst")) {
					GV.marcador.punts+=50;
					base.shoot(true);
				}
				powers.remove(i);
			}
		}
	}
}
